package com.example.navigation

import android.Manifest
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.amap.api.maps.MapsInitializer
import com.amap.api.navi.NaviSetting
import com.example.navigation.api.login.LoginApi
import com.example.navigation.components.MyBottomAppBar
import com.example.navigation.ui.theme.NavigationTheme
import com.example.navigation.viewModel.MapViewModel
import com.example.navigation.views.MapPage
import com.example.navigation.views.MyPage
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        MapsInitializer.updatePrivacyShow(this, true, true)
        MapsInitializer.updatePrivacyAgree(this, true)
        NaviSetting.updatePrivacyShow(this, true, true)
        NaviSetting.updatePrivacyAgree(this, true)
//        AMapUtilCoreApi.setCollectInfoEnable(true)

        val mapViewModel by viewModels<MapViewModel>()


        val sharedPref: SharedPreferences = this@MainActivity.getSharedPreferences(Constants.store_key, Context.MODE_PRIVATE)

        val userStr = sharedPref.getString("user", "")

        var userInfo: LoginApi.UserInfo? = null
        if (userStr!!.isNotEmpty()) {
            userInfo = Gson().fromJson(userStr, LoginApi.UserInfo::class.java)
        }
        // 获取定位权限
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { map ->
            if (map.values.all { it }) {
                Toast.makeText(this, "获取权限成功", Toast.LENGTH_LONG)
                    .show()
            } else {
                Toast.makeText(
                    this, "获取位置失败,没有相应的权限", Toast.LENGTH_LONG
                ).show()
            }
        }.launch(
            arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
        )

        setContent {
            NavigationTheme {
                var pageIndex by remember {
                    mutableIntStateOf(0)
                }
                Scaffold(modifier = Modifier.fillMaxSize(), bottomBar = {
                    MyBottomAppBar {
                        pageIndex = it
                    }
                }) { innerPadding ->
                    Box(
                        modifier = Modifier.padding(innerPadding),
                    ) {
                        AnimatedVisibility(visible = pageIndex == 0) {
                            MapPage(
                                context = applicationContext,
                                savedInstanceState,
                                mapViewModel
                            )
                        }
                        AnimatedVisibility(visible = pageIndex == 1) {
                            MyPage(userInfo, this@MainActivity, sharedPref)
                        }
                    }
                }
            }
        }
    }
}